package com.zhsource.article.controller;


import cn.hutool.core.convert.Convert;
import com.zhsource.article.domain.Article;
import com.zhsource.article.domain.ArticleNum;
import com.zhsource.article.mapper.ArticleMapper;
import com.zhsource.utils.RedisKeyUtils;
import com.zhsource.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tourist/redisLike")
public class RedisLikeController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ArticleMapper articleMapper;


    /**
     * redis 点赞和取消点赞
     *
     * @param articleNum 文章id和用户id
     * @return
     */
    @PostMapping("/spot")
    public Result saveLiked2Redis(@RequestBody ArticleNum articleNum){
        if (ObjectUtils.isEmpty(articleNum.getArticleId()) && ObjectUtils.isEmpty(articleNum.getUserId())){
            return Result.fail("参数传入失败");
        }
        String key = RedisKeyUtils.getLikedKey(articleNum.getArticleId(), articleNum.getUserId());
        //hasKey()非空返回true进入方法  空返回false走else
        if (redisTemplate.opsForHash().hasKey(RedisKeyUtils.MAP_KEY_USER_LIKED,key)){
            if (Integer.parseInt(Convert.toStr(redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED,key)))==1){
                // 取消点赞
                redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, 0);
                // 点赞数-1
                redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, articleNum.getArticleId().toString(), -1L);
                // 数据库点赞数+1
                Article article = articleMapper.selectById(articleNum.getArticleId());
                article.setArticleStarNum(article.getArticleStarNum() + 1);
                articleMapper.update(article);
            }else if (Integer.parseInt(Convert.toStr(redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED,key)))==0) {
                // 点赞
                redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, 1);
                // 点赞数+1
                redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, articleNum.getArticleId().toString(), 1L);
                // 数据库点赞数-1
                Article article = articleMapper.selectById(articleNum.getArticleId());
                article.setArticleStarNum(article.getArticleStarNum() - 1);
                articleMapper.update(article);
            }
        }else {
            // 点赞
            redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, 1);
            // 点赞数+1
            redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, articleNum.getArticleId().toString(), 1L);
            // 数据库点赞数+1
            Article article = articleMapper.selectById(articleNum.getArticleId());
            article.setArticleStarNum(article.getArticleStarNum() + 1);
            articleMapper.update(article);
        }
        int status = Integer.parseInt(Convert.toStr(redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED, key)));
        return Result.success(status);
    }

    /**
     * redis 文章阅读量+1
     *
     * @param articleNum 文章id
     * @return
     */
    @PostMapping("/readAdd")
    public Result incrementLikedCount(@RequestBody ArticleNum articleNum){
        // 文章阅读量+1
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_USER_NEWS_READING_QUANTITY, articleNum.getArticleId().toString(), 1);
        // 数据库文章阅读量+1
        Article article = articleMapper.selectById(articleNum.getArticleId());
        article.setArticleReadCount(article.getArticleReadCount() + 1);
        articleMapper.update(article);
        return Result.success("操作成功");
    }
}